<!--setOrgInfo():返回数据id-->
<!--setOrgname():返回审核人-->
<!--orgTree:true   选择机构-->
<!--codeAffordIf:true   筛选管理机构-->
<!--currentOrgId:orgId   用于修改页面默认展示当前人员的机构ID-->
<!--currentOrgName:orgName   用于修改页面默认展示当前人员的机构Name-->
<!--setCurrentName:{value:123,label:'王德发'}   组件默认显示-->
<!--multiple:true   可多选下级审核人-->
<!--department:true  可选择部门-->
<!--rootOrgId 从该机构id -->

<template>
  <div>
    <Input placeholder="请选择组织机构" v-model="orgName" @on-focus="openOrgModal" readonly suffix="ios-search"/>
    <Modal v-model="modal" width="900" :mask-closable="false" title="组织机构列表" footer-hide>
      <Form :label-width="110" ref="formInline" :model="formInline">
        <Row :gutter="16" class="query-form">
          <i-col span="14">
            <FormItem label="公司名称：">
              <Input class="query-form-el" v-model="formInline.orgName"></Input>
            </FormItem>
          </i-col>
          <!--                    <i-col span="8">-->
          <!--                        <FormItem label="组织机构级别：">-->
          <!--                            <Select class="query-form-el" filterable v-model="formInline.orgLevel" >-->
          <!--                                <Option v-for="item in orgLevelList" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}</Option>-->
          <!--                            </Select>-->
          <!--                        </FormItem>-->
          <!--                    </i-col>-->
          <i-col span="8" offset="8">
            <div style="float: right;">
              <Button class="ivu-btn-reset" @click="resetOrg">重置</Button>
              <Button class="ivu-btn-query" icon="md-search" @click="queryOrg">查询</Button>
            </div>
          </i-col>
        </Row>
      </Form>
      <Table row-key='orgId' ref='table2' height="400" :loading="loading2" no-data-text="没有查询到相关数据!" border
             @on-select="selectionTable"
             :columns="orgColumn" :load-data="handleLoadData" width="100%" :data="orgDatas" size="small">
      </Table>
    </Modal>
  </div>
</template>

<script>
import {sessionStorage} from '../common/cacheUtils.js';
import {Api} from '../api/index.js'

export default {
  name: "cascaderDom",
  props: {
    orgTree: {
      type: Boolean,
      default: false
    },
    codeAffordIf: {
      type: String,
      default: '0'
    },
    multiple: {
      type: Boolean,
      default: false
    },
    department: {
      type: Boolean,
      default: true
    },
    setselectionarr: {
      type: Array,
      default: () => []
    },

    currentOrgId: {},
    currentOrgName: {},
    rootOrgId: null,
    setCurrentName: {},
  },
  data() {
    return {
      modal: false,
      orgDatas: [],
      formInline: {},
      loading2: false,
      orgLevelList: [],
      orgColumn: [
        {
          type: 'selection',
          width: 60,
          align: 'center',
        },
        {
          title: '组织机构名称',
          minWidth: 200,
          align: 'left',
          key: 'orgName',
          tree: true,
          render: (h, params) => {
            return h('span', params.row.orgName + ((params.row.orgType=='1'||params.row.orgLevel=='10'||params.row.orgLevel=='0200100005000000001') ? '':('(' + params.row.company_name + ')') ));
          }
        },
        {
          title: '组织机构级别',
          width: 150,
          align: 'center',
          key: 'orgLevel',
          render: (h, params) => {
            return h('span', this.orgLevelMap[params.row.orgLevel]);
          }
        },
      ],
      orgName: this.setCurrentName ? this.setCurrentName.label : '',
      orgLevelMap: {},
    }
  },
  methods: {
    selectCodingSort() {
      this.$http.get(Api.queryCodeDetailBySortId + '/0200100005', {params: {}}).then((res) => {
        this.orgLevelList = res.data.data;
        this.orgLevelList.forEach(item => {
          this.orgLevelMap[item.codingCodeId] = item.codingName;
        })
      });
    },
    setOrgName(orgName) {
      this.orgName = orgName;
    },
    openOrgModal() {
      this.modal = true;
      this.getOrgInfo();
    },
    getOrgInfo() {
      let orgId = this.rootOrgId;
      if (orgId == null) {
        orgId = sessionStorage('userId').getObject('userId').companyId
      }
      this.$http.get(Api.queryOrgDetailById + "/" + orgId).then(res => {
        this.orgDatas = [];
        let temp = {};
        temp._loading = false;
        temp.children = [];
        temp.orgName = res.data.data.orgName;
        temp.orgLevel = res.data.data.orgLevel;
        temp.orgType = res.data.data.orgType;
        temp.orgId = res.data.data.orgId;
        this.orgDatas.push(temp);
      }).catch(e => {
        this.$Message.error('请求失败');
      })
    },
    handleLoadData(item, callback) {
      setTimeout(() => {
        let data = {
          parentId: item.orgId,
          codeAffordIf: 0,

        }
        if (!this.department) {
          data.orgType = '1'
        }
        this.$http.get(Api.queryOrg, {
          params: data
        }).then(res => {
          if (res.data.data.length !== 0) {
            res.data.data.map(i => {
              if (i.orgLevel != '0200100005000000007') {
                i._loading = false;
                i.children = [];
              }
            })
            item.children = res.data.data;
            callback(item.children);
          } else {
            delete item['_loading'];
            item.children = [];
            callback([]);
          }
        }).catch(e => {
          this.$Message.error('请求失败');
        })
      }, 2000);
    },
    selectionTable(selection, row) {
      this.modal = false;
      this.orgName = row.orgName;
      let data = [row.orgId];
      this.$emit('setOrgInfo', data);
      this.$emit('setOrgname', row.orgName);
      this.$emit('setOrgCode', row.orgCode);
    },
    resetOrg() {
      this.formInline.orgName = '';
      this.formInline.orgLevel = '';
      this.getOrgInfo();
    },
    queryOrg() {

      let params = {
        fromPage: 1,
        pageSize: 1000,
        orgName: this.formInline.orgName
      }
      if (!this.department) {
        params.orgType = '1'
      }
      if (this.formInline.orgName) {
        this.loading2 = true;
        this.$http.get(Api.queryOrgList, {params: params}).then(res => {
          let datas = [];
          res.data.data.map(i => {
//            if (i.orgLevel == '13' || i.orgLevel == '12') {
//              return;
//            }
            if (i.orgLevel != '0200100005000000007') {
              i._loading = false;
              i.children = [];
            }
            datas.push(i);
          })
          this.orgDatas = datas;
          this.loading2 = false;
        }).catch(e => {
          this.$Message.error('请求失败');
          this.loading2 = false;
        })
      } else {
        this.$Message.warning("请输入公司名称")
      }

    },
    resetCurrentOrg() {
      this.formInline.orgName = '';
      this.formInline.orgLevel = '';
      this.orgName = '';
    },
    setCurrentOrg() {
      if (this.setCurrentName != undefined) {
        this.orgName = this.setCurrentName.label;
      } else {
        this.orgName = this.currentOrgName;
      }
    },
    cancel() {
      this.formInline = {
        orgName: ''
      };
      this.queryOrg();
    }
  },
  computed: {},
  mounted() {

    this.isModal = false;
  }
}
</script>

<style lang="less">
/*修改分页样式*/

.Cascader {
  display: block;
}
</style>
